package com.aliware.csb;

import com.alibaba.csb.sdk.HttpCaller;
import com.alibaba.csb.sdk.HttpCallerException;
import com.alibaba.csb.sdk.HttpParameters;
import com.alibaba.csb.sdk.HttpReturn;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class CallCSBController {

    private static final String url = "https://shared.csb.cn-beijing.aliyuncs.com:8086/csb/echoString";
    private static final String apiName = "echoString";
    private static final String version = "1.1.0";


    //security related params
    private static final String ak = "e35b5d8e0131448f9660a0f55dec70ee";
    private static final String sk = "WtB+tnZQCpU4SgGx4n3ucabcpkk=";



    @RequestMapping(value = "/call-csb/{str}", method = RequestMethod.GET)
    public String echo(@PathVariable String str) throws HttpCallerException {
        System.out.println("testJson request url:" + url);
        System.out.println("apiName:" + apiName);
        System.out.println("ak:" + ak);
        System.out.println("sk:" + sk);
        System.out.println("str:" + str);

        HttpParameters.Builder builder = HttpParameters.newBuilder();
        builder.requestURL(url).api(apiName).version(version).method("get").accessKey(ak).secretKey(sk);

        builder.putParamsMap("str", str);

        //进行调用 返回结果
        String result = null;
        try {
//            result = HttpCaller.invoke(builder.build());
            // 或者使用下面的调用方法, 可以返回更多的信息，包括诊断信息,这个功能仅在SDK1.1.4.0以后版本支持
//            HttpReturn res = HttpCaller.invokeReturn(builder.build()); //然后在res里获取相关的信息


            // 如果期望获取返回的http headers, 则需要在invoke中加入第二个参数，如下：
            // 用这个参数变量获取调用后返回的http headers, 内容为
            //   {"header1":"value1","header2":"value2",...}
            StringBuffer resHttpHeaders = new StringBuffer();
            result = HttpCaller.invoke(builder.build(), resHttpHeaders);

            System.out.println("resHttpHeaders:" + resHttpHeaders.toString());


            //注：如果返回结果出现乱码(不能正常显示中文),可以使用串字符集转换方法进行转换
            result = HttpCaller.changeCharset(result);
        } catch (HttpCallerException e) {
            // error process
        }

        return result;
    }


    @RequestMapping(value = "/call-csb-post", method = RequestMethod.GET)
    public String echoPost() throws HttpCallerException {
        HttpParameters.Builder builder = HttpParameters.newBuilder();
        builder.requestURL("https://shared.csb.cn-beijing.aliyuncs.com:8086/csb/echoBean")
                .api("echo")
                .version(version)
                .method("post")
                .accessKey(ak)
                .secretKey(sk);

        builder.putParamsMap("echoBean", "{\"len\":10,\"message\":\"123456sdasdsadasd789\"}"); // json format value
        String result = null;
        try{

            StringBuffer resHttpHeaders = new StringBuffer();
            result = HttpCaller.invoke(builder.build(), resHttpHeaders);

            System.out.println("resHttpHeaders:" + resHttpHeaders.toString());


            //注：如果返回结果出现乱码(不能正常显示中文),可以使用串字符集转换方法进行转换
            result = HttpCaller.changeCharset(result);
        } catch (HttpCallerException e) {
            // error process
        }

        return result;
    }

    @RequestMapping(value = "/call-csb-local-delay", method = RequestMethod.GET)
    public String localFilghtDelay() throws HttpCallerException {
        HttpParameters.Builder builder = HttpParameters.newBuilder();
        builder.requestURL("https://shared.csb.cn-beijing.aliyuncs.com:8086/csb/flightInformation")
                .api("FlightDelay")
                .version(version)
                .method("get");
//                .accessKey(ak)
//                .secretKey(sk);

        builder.putParamsMap("flightNumber", "H5783"); // json format value
        String result = null;
        try{

            StringBuffer resHttpHeaders = new StringBuffer();
            result = HttpCaller.invoke(builder.build(), resHttpHeaders);

            System.out.println("resHttpHeaders:" + resHttpHeaders.toString());

            System.out.println("result:" + result);


            //注：如果返回结果出现乱码(不能正常显示中文),可以使用串字符集转换方法进行转换
//            result = HttpCaller.changeCharset(result);
        } catch (HttpCallerException e) {
            // error process
        }

        return result;
    }

    @RequestMapping(value = "/call-csb-delay", method = RequestMethod.GET)
    public String filghtDelay() throws HttpCallerException {
        HttpParameters.Builder builder = HttpParameters.newBuilder();
        builder.requestURL("http://60.205.210.11:8086/csb/flightInformation")
                .api("FlightDelayAPI")
                .version(version)
                .method("get");
//                .accessKey("ee3eee1da0cc42b7a69ea009e62595c8")
//                .secretKey("Sj8AGFEKJMR+roKe03Tdw6IR6G8=");

//        builder.requestURL("http://172.17.124.225:8086/csb/flightInformation")

        builder.putParamsMap("flightNumber", "H5783"); // json format value
        String result = null;
        try{

            StringBuffer resHttpHeaders = new StringBuffer();
            result = HttpCaller.invoke(builder.build(), resHttpHeaders);

            System.out.println("resHttpHeaders:" + resHttpHeaders.toString());



            //注：如果返回结果出现乱码(不能正常显示中文),可以使用串字符集转换方法进行转换
//            result = HttpCaller.changeCharset(result);
        } catch (HttpCallerException e) {
            // error process
        }

        return result;
    }
}
